import sys
from codecs import open
from xml.dom.minidom import parse
import time

if len(sys.argv)!=2:
  print '''
Add unix time into attr of created_at node
Usage: %s <log-file>
''' % (sys.argv[0])

else:
  dom=parse(sys.argv[1])
  print 'loaded '+sys.argv[1]
  elems=dom.getElementsByTagName('created_at')
  for elem in elems:
    if not elem.hasAttribute('epoch'):
      ds=elem.firstChild.data
      dd=time.strptime(ds, '%a %b %d %H:%M:%S +0000 %Y')
      dds=str(int(time.mktime(dd)))
      elem.setAttribute('epoch', dds)
  print 'modified created_at nodes'
  path=sys.argv[1]+'.addepoch.xml'
  f=open(path, 'w', 'utf-8')
  dom.writexml(f)
  print 'wrote '+path
  
